package com.cxm.weilai.util;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * 测试交行
 * @author jackie.cheng
 */
public class CommunicationBankPayUtil {
	private static Log logger = LogFactory.getLog(CommunicationBankPayUtil.class);

    public final static String AP = "AP"; //付款人账号C40_Y
    public final static String BODY = "BODY";
    public final static String PAY_ACNO = "PAY_ACNO"; //付款人户名C60_Y
    public final static String PAY_ACNAME = "PAY_ACNAME"; //收款方行名C60_Y
    public final static String RCV_BANK_NAME = "RCV_BANK_NAME"; //收款人账号C40_Y
    public final static String RCV_ACNO = "RCV_ACNO"; //收款人账号C40_Y
    public final static String RCV_ACNAME = "RCV_ACNAME"; //收款人户名C60_Y
    public final static String RCV_EXG_CODE = "RCV_EXG_CODE"; //收款方交换号C10
    public final static String RCV_BANK_NO = "RCV_BANK_NO"; //收款方联行号C12
    public final static String CUR_CODE = "CUR_CODE"; //币种C3_Y 仅限CNY
    public final static String AMT = "AMT"; //金额N14.2_Y
    public final static String CERT_NO = "CERT_NO"; //企业凭证编号C20_Y
    public final static String SUMMARY = "SUMMARY"; //附言C60_N 58字符，不包含特殊符号
    public final static String BANK_FLAG = "BANK_FLAG"; //银行标志C1_Y,指收款方账户的开户行所在地0：交行1：他行
    public final static String AREA_FLAG = "AREA_FLAG"; //同城异地标志C1_Y,指付款方所在地和收款方所在地是否为同城0：同城1：异地

    /**
     * 根据参数构建请求对外转账交易报文(210201)(目前仅支持人民币)
     * <ap>
        <body>
            <pay_acno>付款人账号C40_Y</pay_acno>
            <pay_acname>付款人户名C60_Y</pay_acname>
            <rcv_bank_name>收款方行名C60_Y</rcv_bank_name>
            <rcv_acno>收款人账号C40_Y</rcv_acno>
            <rcv_acname>收款人户名C60_Y</rcv_acname>
            <rcv_exg_code>收款方交换号C10</rcv_exg_code>
            <rcv_bank_no>收款方联行号C12</rcv_bank_no>
            <cur_code>币种C3_Y 仅限CNY</cur_code>
            <amt>金额N14.2_Y</amt>
            <cert_no>企业凭证编号C20_Y</cert_no>
            <summary>附言C60_N 58字符，不包含特殊符号</summary>
            <bank_flag>银行标志C1_Y,指收款方账户的开户行所在地0：交行1：他行</bank_flag>
            <area_flag>同城异地标志C1_Y,指付款方所在地和收款方所在地是否为同城0：同城1：异地</area_flag>
        </body>
       </ap>
     */
    public static String buildrOutwardDealMessage(Map<String, String> params) {
        Document document = DocumentHelper.createDocument();
        Element rootElement = document.addElement(AP);
        Element bodyElement = rootElement.addElement(BODY);
        bodyElement.addElement(PAY_ACNO).addText(params.get(PAY_ACNO));
        bodyElement.addElement(PAY_ACNAME).addText(params.get(PAY_ACNAME));
        bodyElement.addElement(RCV_BANK_NAME).addText(params.get(RCV_BANK_NAME));
        bodyElement.addElement(RCV_ACNO).addText(params.get(RCV_ACNO));
        bodyElement.addElement(RCV_ACNAME).addText(params.get(RCV_ACNAME));
        bodyElement.addElement(RCV_EXG_CODE).addText(params.get(RCV_EXG_CODE));
        bodyElement.addElement(RCV_BANK_NO).addText(params.get(RCV_BANK_NO));
        bodyElement.addElement(CUR_CODE).addText(params.get(CUR_CODE));
        bodyElement.addElement(AMT).addText(params.get(AMT));
        bodyElement.addElement(CERT_NO).addText(params.get(CERT_NO));
        bodyElement.addElement(SUMMARY).addText(params.get(SUMMARY));
        bodyElement.addElement(BANK_FLAG).addText(params.get(BANK_FLAG));
        bodyElement.addElement(AREA_FLAG).addText(params.get(AREA_FLAG));

        return convertDocumentToString(document);
    }

    /**
     * 转换document to string
     */
    public static String convertDocumentToString(Document document) {
        Writer writer = new StringWriter();
        OutputFormat format = new OutputFormat();
        format.setSuppressDeclaration(true);
        XMLWriter xmlWriter = new XMLWriter(writer, format);
        try {
            xmlWriter.write(document);
        } catch (IOException e1) {
            logger.info("Fail to convert the document to string.");
            return null;
        }

        return writer.toString();
    }

    /**
     * 模拟测试
     */
    public static void main(String[] args) {
//        Map<String, String> mapParams = new MapParamsBuilder().addParamValue(PAY_ACNO, "310066030018170127718")
//                .addParamValue(PAY_ACNAME, "交行客户")
//                .addParamValue(RCV_BANK_NAME, "")
//                .addParamValue(RCV_ACNO, "310066030018170128388")
//                .addParamValue(RCV_ACNAME, "交行客户")
//                .addParamValue(RCV_EXG_CODE, "")
//                .addParamValue(RCV_BANK_NO, "")
//                .addParamValue(CUR_CODE, "")
//                .addParamValue(AMT, "888.88")
//                .addParamValue(CERT_NO, "0000034912")
//                .addParamValue(CERT_NO, "0000034912")
//                .addParamValue(SUMMARY, "测试付款")
//                .addParamValue(BANK_FLAG, "0")  //指收款方账户的开户行所在地0：交行1：他行
//                .addParamValue(AREA_FLAG, "0")  //指付款方所在地和收款方所在地是否为同城0：同城1：异地
//                .getMapParams();
//
//        String requestMessage = CommunicationBankPayUtil.buildrOutwardDealMessage(mapParams);
        //System.out.println(requestMessage);
    }
}
